package gdconf

import (
	"server_cluster/common/logger"
	"strconv"
)

const (
	PARENT_QUEST_ACCEPT_COND_LIST_MAX = 3
	QUEST_MAX                         = 1000
)

// 抽卡蛋池配置表
type ConfigParentQuest struct {
	Id             int32         `csv:"ID" gorm:"primary_key;autoIncrement:false"` // 主线任务id
	Type           int32         `csv:"Type,omitempty"`                            // 任务类型
	QuestList      IntArray      `csv:"Finish_condition,omitempty"`                // 子任务列表
	Reward         int32         `csv:"Reward,omitempty"`                          // 主线任务奖励id
	AcceptCondList IntArrayArray `csv:"AcceptConditionList,omitempty"`             // 接取条件
	TrialRoleList  IntArray      `csv:"TrialGroup,omitempty"`                      //试用角色组 [场景id,试用角色组id]
}

func (t *ConfigParentQuest) TableName() string {
	return "config_parent_quest"
}

func (g *GameDataConfig) saveParentQuest() {
	var taskList = readTable[ConfigParentQuest](g.CsvPathPrefix + "TaskMain.csv")
	taskList = append(taskList, readTable[ConfigParentQuest](g.CsvPathPrefix+"TaskSide.csv")...)
	taskList = append(taskList, readTable[ConfigParentQuest](g.CsvPathPrefix+"TaskDaily.csv")...)
	saveTableToDb[ConfigParentQuest](g.Db, taskList)
}

func (g *GameDataConfig) loadParentQuestConfig() {
	g.GameDataMaps.ParentQuestMap = make(map[int32]*ConfigParentQuest)
	for _, parentQuest := range loadTableFromDb[ConfigParentQuest](g.Db) {
		g.GameDataMaps.ParentQuestMap[parentQuest.Id] = parentQuest
		for _, questId := range parentQuest.QuestList {
			questConfig := GetConfigQuest(questId)
			if questConfig == nil {
				panic("loadParentQuestConfig failed questId1 : " + strconv.Itoa(int(questId)))
			}
			questConfig.ParentQuestId = parentQuest.Id
			if questConfig.ParentQuestId == 0 {
				logger.Info("loadParentQuestConfig failed questId2 : " + strconv.Itoa(int(questId)))
			}
		}
	}
	logger.Info("ConfigParentQuest Count: %v", len(g.GameDataMaps.ParentQuestMap))
}

func GetConfigParentQuestMap() map[int32]*ConfigParentQuest {
	ret := make(map[int32]*ConfigParentQuest)
	for _, v := range CONF.GameDataMaps.ParentQuestMap {
		ret[v.Id] = v
	}
	return ret
}

func GetConfigParentQuest(id int32) *ConfigParentQuest {
	v, ok := CONF.GameDataMaps.ParentQuestMap[id]
	if !ok {
		return nil
	}
	return v
}

func GetTrailRoleGroupIdBySceneId(sceneId, questId int32) int32 {
	var questConfig = GetConfigParentQuest(questId)
	if questConfig == nil {
		logger.Debug("GetTrailRoleGroupIdBySceneId failed questId : " + strconv.Itoa(int(questId)))
		return 0
	}
	if len(questConfig.TrialRoleList) < 2 {
		logger.Debug("GetTrailRoleGroupIdBySceneId trailRoleList err; param num not match!")
		return 0
	}
	if questConfig.TrialRoleList[0] == sceneId {
		return questConfig.TrialRoleList[1]
	}
	return 0
}
